package com.atguigu.gulimail.ware.service.impl;

import com.atguigu.common.constant.WareContant;
import com.atguigu.gulimail.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimail.ware.entity.Vo.doneVo;
import com.atguigu.gulimail.ware.entity.Vo.itemVo;
import com.atguigu.gulimail.ware.entity.Vo.mergeVo;
import com.atguigu.gulimail.ware.service.PurchaseDetailService;
import com.atguigu.gulimail.ware.service.WareSkuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.ware.dao.PurchaseDao;
import com.atguigu.gulimail.ware.entity.PurchaseEntity;
import com.atguigu.gulimail.ware.service.PurchaseService;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;

@Slf4j
@Service("purchaseService")
public class PurchaseServiceImpl extends ServiceImpl<PurchaseDao, PurchaseEntity> implements PurchaseService {

    @Autowired
    PurchaseDetailService detailService;
    @Autowired
    WareSkuService wareSkuService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),
                new QueryWrapper<PurchaseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageUnreceiced(Map<String, Object> params) {

        LambdaQueryWrapper<PurchaseEntity> wrapper=new LambdaQueryWrapper<>();

        wrapper.eq(PurchaseEntity::getStatus,0).or().eq(PurchaseEntity::getStatus,1);

        IPage<PurchaseEntity> page = this.page(
                new Query<PurchaseEntity>().getPage(params),wrapper

        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void mergePurchase(mergeVo vo) {
        Long purchaseId=vo.getPurchaseId();
        if (purchaseId==null){
            PurchaseEntity purchaseEntity = new PurchaseEntity();
            purchaseEntity.setStatus(WareContant.PurchaseStatusEnum.CREATED.getCode());
            purchaseEntity.setCreateTime(new Date());
            purchaseEntity.setUpdateTime(new Date());
            this.save(purchaseEntity);
            purchaseId=purchaseEntity.getId();
        }

        Long finalPurchseId=purchaseId;
        List<Long> item=vo.getItems();
        List<PurchaseDetailEntity> collect = item.stream().map(
                i -> {
                    PurchaseDetailEntity purchaseDetailEntity = new PurchaseDetailEntity();
                    purchaseDetailEntity.setId(i);
                    purchaseDetailEntity.setPurchaseId(finalPurchseId);
                    purchaseDetailEntity.setStatus(WareContant.PurchaseDetailStatusEnum.ASSIGEND.getCode());
                    return purchaseDetailEntity;
                }
        ).collect(Collectors.toList());
        detailService.updateBatchById(collect);

        PurchaseEntity entity = new PurchaseEntity();
        entity.setId(finalPurchseId);
        entity.setUpdateTime(new Date());
        this.updateById(entity);
    }

    @Override
    public void recevied(List<Long> purIds) {
        //首先确认Id是新建或者是已分配
        List<PurchaseEntity> collect = purIds.stream().map(i -> {
            PurchaseEntity purchase = this.getById(i);
            return purchase;
        }).filter(item -> {
            if (item.getStatus() == WareContant.PurchaseStatusEnum.CREATED.getCode()
                    || item.getStatus() == WareContant.PurchaseStatusEnum.ASSIGEND.getCode()) {
                return true;
            };

            return false;
        }).map(item->{
            //过滤完后将状态更改为已被领取
            item.setStatus(WareContant.PurchaseStatusEnum.RECEIVE.getCode());
            item.setUpdateTime(new Date());
            return item;
        }).collect(Collectors.toList());

        this.updateBatchById(collect);
        
        //改变采购单中的采购项的状态为已领取
        for (PurchaseEntity p:collect
             ) {
            List<PurchaseDetailEntity> detailEntityList= detailService.listDelailByPurchaseId(p.getId());
            List<PurchaseDetailEntity> detailEntityStream = detailEntityList.stream().map(i -> {
                i.setStatus(WareContant.PurchaseDetailStatusEnum.BUYING.getCode());
                return i;
            }).collect(Collectors.toList());
            detailService.updateBatchById(detailEntityStream);

        }
    }

    @Override
    @Transactional
    public void done(doneVo done) {
        log.info(done.toString());
        //改变采购单的状态
        Long id=done.getId();
        //改变采购单的采购项的状态
        Boolean flage=true; //采购项目都完成的标志位
        List<itemVo> itemVos=done.getItems();
        List<PurchaseDetailEntity> detailEntitys=new ArrayList<>();//保存完成的采购项

        for (itemVo item:itemVos) {
            PurchaseDetailEntity detailEntity = new PurchaseDetailEntity();

            if (item.getStatus()==WareContant.PurchaseDetailStatusEnum.HASERROR.getCode()){
                detailEntity.setStatus(item.getStatus());
                flage=false;
            }
            if(item.getStatus()==WareContant.PurchaseDetailStatusEnum.FINISH.getCode()){
                detailEntity.setStatus(WareContant.PurchaseDetailStatusEnum.FINISH.getCode());
                //将成功采购的项入库
                PurchaseDetailEntity detailServiceById = detailService.getById(item.getItemId());
                log.info("---------------"+detailServiceById.toString());
                wareSkuService.addStock(detailServiceById);
            }
            detailEntity.setId(item.getItemId());
            detailEntitys.add(detailEntity);

        }
        detailService.updateBatchById(detailEntitys);
        PurchaseEntity purchaseEntity = new PurchaseEntity();
        purchaseEntity.setId(id);
        purchaseEntity.setUpdateTime(new Date());
        purchaseEntity.setStatus(flage?WareContant.PurchaseStatusEnum.FINISH.getCode():WareContant.PurchaseStatusEnum.HASERROR.getCode());
        this.updateById(purchaseEntity);


    }

}